in C++ function can be overload, the same name may be from different function.
function call: initialize the function`s parameter, transfer control to the function.
local static object is initialized before the first execution ,not destroyed untill the function ends.
Passing argument by value:the value of the initiaizer is copied,changes made to the variable have no effect on the initialzizer.
Pointer Parameter :copy the value of another pointer,but can change the value of the object by assignning through the pointer.
Passing Argument byreference:changes made to the reference are made to the object to which referencr refers.
Using Reference Parameters to Return Additional Information:
return multiple results effectively.
const Parameter and Arguments:top-level const that applt to the object itself.
Two Way to array:
- print(const int beg, const int end)
- print(const int ia[], size_t size)
|
|
initializer_list: all const value ,no way to change the value of an element.
begin():a pointer to the first element.
end():an off-the-end pointer one past the last element.
**Swap In Reference
return reference must not refer to an local object,as well as pointer.
Calls to functions that return references are lvalues; other return types
yield rvalues
Overlaod function
Functions that have the same name but different parameter lists and that appear in the same scope are overloaded
Overload function must differ in hte number or the types of their aprameters.
Top-level const has no effect on the objects that can be passed to the function.
function taking const and nonconst reference or pointers have different parameters.
In C++, name lookup happens before type checking.
the compiler first looks for a declaration of that name.
Once a name is found, the compiler
ignores uses of that name in any outer scope’
Default Argument Declarations
any subsequent declaration can add a
default only for a parameter that has not previously had a default specified
Inline Function:remove the run-time of calling a function.
put the key word inline before the return type.
A constexpr function is not required to return a constant expression.
assert(expr);
evaluate expr and if the expression is false, then assert writes a message and terminates the program.
NDEBUG
if NDEBUG is defined assert does nothing.
if NDEBUG is not defined, the code between #ifndef and the #endif is executed.
Function match
- First step:identifies the set of overloaded functions considered for the call. a function with the same name as the called function and for
which a declaration is visible at the point of the call - Second step:select viable function. have the same number of parameters as there are arguments in the call. and the type of each arguement must match -or be convertible to.
- third step: select the viable function for which the corresponding parameter best match the arguement.
Casts should not be needed to call an overloaded function. The need for a
cast suggests that the parameter sets are designed poorly.
Pointer to Function
When we use the name of a function as a value, the function is automatically converted to a pointer
pointers to different function type can`t conversion,but can be assigned nullptr or a zero-valued integer constant expression.
When we pass a function as an argument, we can do so directly. It will be
automatically converted to a pointer